package com.kyzh.core.activities

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.setPadding
import androidx.databinding.DataBindingUtil
import com.google.android.material.tabs.TabLayout
import com.gushenge.core.base.Base
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.requests.LiveRequest
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ActivityMyassets1Binding
import com.kyzh.core.databinding.ItemSettingsV3Binding
import com.kyzh.core.databinding.TablayoutGuanzhuTextBinding
import com.kyzh.core.utils.httpSign
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.time

class
MyAssets1Activity : BaseActivity() {
    private lateinit var binding: ActivityMyassets1Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_myassets1)
        initView()
    }
    private var tabs = arrayListOf<String>("游戏","直播")
    private fun initView() {

        binding?.apply {
            tab.setVisibility(false)
            tabs.forEach {
                if(tab.childCount>tabs.size){
                    return
                }
                tab.addTab(tab.newTab().apply {

                    customView = null
                    val inflate = DataBindingUtil.inflate<TablayoutGuanzhuTextBinding>(
                        LayoutInflater.from(this@MyAssets1Activity),
                        R.layout.tablayout_guanzhu_text,
                        null,
                        false
                    )
                    tag = it
                    val findViewById = inflate.root.findViewById<TextView>(R.id.tv)
                    findViewById.text = it
                    customView = inflate.root
                    customView?.setPadding(0)
                })
            }
            tab.getTabAt(0)?.select()

            tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val findViewById = tab?.customView?.findViewById<TextView>(R.id.tv)
                    findViewById?.setTextColor(resources.getColor(R.color.font_33))
                    findViewById?.textSize = 16f
                    if (tab != null) {
                        if(tab.tag == tabs[1]){
                            lin2.setVisibility(true)
                            lin1.setVisibility(false)
                        }else{
                            lin2.setVisibility(false)
                            lin1.setVisibility(true)
                        }
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    val findViewById = tab?.customView?.findViewById<TextView>(R.id.tv)
                    findViewById?.setTextColor(resources.getColor(R.color.font_99))
                    findViewById?.textSize = 14f
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                    val findViewById = tab?.customView?.findViewById<TextView>(R.id.tv)
                    findViewById?.setTextColor(resources.getColor(R.color.font_33))
                    findViewById?.textSize = 16f
                    if (tab != null) {
                        if(tab.tag == tabs[1]){
                            lin2.setVisibility(true)
                            lin1.setVisibility(false)
                        }else{
                            lin2.setVisibility(false)
                            lin1.setVisibility(true)
                        }
                    }
                }
            })
        }
        LiveRequest.getWallet{
            this?.apply {
                binding.also {
                    it.zuan.setInfo(R.drawable.ic_pingtaibi,"钻石余额",zuan.num)
                    it.zuan.root.setOnClickListener {
                        this@MyAssets1Activity.startActivity<BrowserActivity>(GlobalKeys.TITLE to "", GlobalKeys.LINK to zuan.url)
                    }
                    it.yongjin.setInfo(R.drawable.ic_yongjin,"礼物佣金",shouyi.num)
                    it.yongjin.root.setOnClickListener {
                        this@MyAssets1Activity.startActivity<BrowserActivity>(GlobalKeys.TITLE to "", GlobalKeys.LINK to shouyi.url)
                    }
                }
            }

        }

        // 第一页
        UserRequest.myAssets {
            if(top == 1){
                binding.tab.setVisibility(true)
            }else{
                binding.tab.setVisibility(false)
            }
            binding.also {
                it.vCoin.setInfo(R.drawable.ic_myassets_coin,getString(R.string.ptb),coin)
                it.vCoin.root.setOnClickListener {
                    this@MyAssets1Activity.startActivity<BrowserActivity>(GlobalKeys.TITLE to getString(R.string.ptbRecharge), GlobalKeys.LINK to "${Base.BASEURLV3}/?ct=coin&uid=${MMKVConsts.uid}&t=$time&sign=${httpSign(MMKVConsts.uid)}")
                }
                it.vCoupon.setInfo(R.drawable.ic_myassets_coupon,getString(R.string.coupon),String.format(getString(R.string.couponNum),coupon))
                it.vCoupon.root.setOnClickListener {
                    startActivity<MyCouponActivity>()
                }
                it.vGift.setInfo(R.drawable.ic_myassets_gift,getString(R.string.gift),String.format(getString(R.string.giftNum),lb))
                it.vGift.root.setOnClickListener {
                    startActivity<MyGiftActivity>()
                }
                it.vTiedMoney.setInfo(R.drawable.ic_myassets_tiedmoney,getString(R.string.tiedMoney), String.format(getString(R.string.gameNum),bind))
                it.vTiedMoney.root.setOnClickListener {
                    startActivity<TiedMoneyActivity>()
                }
                it.vPoint.setInfo(R.drawable.ic_myassets_point,getString(R.string.pointText),points)
                it.vPoint.root.setOnClickListener {
                    startActivity<PointsMallActivity>()
                }
                it.vMyDh.setInfo(R.drawable.ic_myassets_order,getString(R.string.exchangedProduct), String.format(getString(R.string.productNums),order))
                it.vMyDh.root.setOnClickListener {
                    startActivity<MyOrderActivity>()
                }
            }
        }
    }

    private fun ItemSettingsV3Binding.setInfo(icon:Int,title:String,  desc:String=""){

        this.tvStart.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(this@MyAssets1Activity,icon),null,null,null)
        this.text = title
        this.desc = desc
    }
}